DuellCounter = inherit(Singleton)

function DuellCounter:constructor()
	self.m_Frame = CreateFrame("Frame",nil)
	self.m_Duell = false
	self.m_Frame:SetScript("onEvent",bind(self.onScript,self))
	self.m_Frame:RegisterEvent("CHAT_MSG_SYSTEM")
	self.m_Frame:RegisterEvent("CHAT_MSG_WHISPER")
	SLASH_DUELLCOUNTER1 = "/resetduel"
	SLASH_DUELLCOUNTER2 = "/rduel"
	SLASH_DUELLCOUNTER3 = "/rd"
	
	utFooDB.takenplaceDuells = utFooDB.takenplaceDuells or {}
	
	function SlashCmdList.DUELLCOUNTER()
		utFooDB.duell_win  = 0 
		utFooDB.duell_lose = 0
		utFooDB.duell_fled = 0
		print("[DC] DuelCounter resetted.")
	end
end

function DuellCounter:CHAT_MSG_SYSTEM(msg)
	--Now check win/lose possibilities
	if msg:find(CLIENT_NAME.." has defeated %u%l*-%u%a* in a duel") or msg:find(CLIENT_NAME.." has defeated %u%l* in a duel") then
		utFooDB.duell_win = 1 + ( utFooDB.duell_win == nil and 0 or utFooDB.duell_win)
		self:addToLastDuells(self:filterEnemy(msg),"win")
	elseif msg:find("%u%l*-%u%a* has defeated ".. CLIENT_NAME .." in a duel") or msg:find("%u%l* has defeated ".. CLIENT_NAME .." in a duel") then
		utFooDB.duell_lose = 1 + ( utFooDB.duell_lose == nil and 0 or utFooDB.duell_lose)
		self:addToLastDuells(self:filterEnemy(msg),"lose")
	elseif msg:find(CLIENT_NAME.." has fled from %u%l*-%u%a* in a duel") or msg:find("%u%l*-%u%a* has fled from ".. CLIENT_NAME .." in a duel")
	or msg:find(CLIENT_NAME.." has fled from %u%l* in a duel") or msg:find("%u%l* has fled from ".. CLIENT_NAME .." in a duel") then
		utFooDB.duell_fled = 1 + ( utFooDB.duell_fled == nil and 0 or utFooDB.duell_fled)
		self:addToLastDuells(self:filterEnemy(msg),"fled")
	end
end
	
function DuellCounter:filterEnemy(msg)
	local words = string.split(msg," ")
	for _, word in ipairs(words) do
		if word ~= CLIENT_NAME then
			if UnitExists(word) then
				return word
			end
		end
	end
end

function DuellCounter:addToLastDuells(enemy,possi)
	if not enemy then
		return
	end
	local oldDuells = utFooDB.takenplaceDuells
	utFooDB.takenplaceDuells = {}
	table.insert(utFooDB.takenplaceDuells, { ENEMY = enemy, POSSI = possi })
	table.foreach(oldDuells, function ( _, value ) table.insert(utFooDB.takenplaceDuells,value) end)
	if #utFooDB.takenplaceDuells > ADDON_DUELL_SAVE_MAX then
		utFooDB.takenplaceDuells[ADDON_DUELL_SAVE_MAX+1] = nil
	end
end

function DuellCounter:CHAT_MSG_WHISPER(msg,author)
	if msg:sub(0,#(ADDON_COMMAND_SHOWDUELL)):find(ADDON_COMMAND_SHOWDUELL) then
		SendChatMessage(ADDON_DUELL_WINS:format(utFooDB.duell_win or 0),"WHISPER",GetDefaultLanguage("player"),author)
		SendChatMessage(ADDON_DUELL_LOSE:format(utFooDB.duell_lose or 0),"WHISPER",GetDefaultLanguage("player"),author)
		SendChatMessage(ADDON_DUELL_FLED:format(utFooDB.duell_fled or 0),"WHISPER",GetDefaultLanguage("player"),author)
	end
end

function DuellCounter:onScript(_,event,...)
	if self[event] then
		self[event](self,...)
	end
end

function DuellCounter:destructor()
end